<!DOCTYPE html>
<html style="height: 100%">
<head>
    <meta charset="UTF-8">
    <title>ECharts Dynamic Charts</title>
    <!-- 引入 ECharts 文件 -->
    <script src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script>
</head>
<body style="height: 100%; margin: 0">
<!-- 为 ECharts 准备一个具备大小（宽高）的 DOM -->
<div id="main" style="height: 100%"></div>
<script type="text/javascript">
    // 基于准备好的dom，初始化echarts实例
    var myChart = echarts.init(document.getElementById('main'));

    // 定义原始数据
    const rawData = [
        { name: 'Mon', value: 10 },
        { name: 'Tue', value: 20 },
        { name: 'Wed', value: 30 },
        { name: 'Thu', value: 40 },
        { name: 'Fri', value: 50 },
        { name: 'Sat', value: 60 },
        { name: 'Sun', value: 70 }
    ];

    function transformDataForChart(dataType, rawData) {
        switch (dataType) {
            case 'line':
            case 'bar':
                return rawData.map(item => ({
                    name: item.name,
                    value: item.value
                }));
            case 'pie':
                return rawData.map((item, index) => ({
                    name: `${item.name} (${index})`,
                    value: item.value
                }));
            case 'scatter':
                return rawData.map(item => ([item.value, item.value]));
            case 'map':
                return rawData.map(item => ({
                    name: item.name,
                    value: item.value
                }));
            default:
                throw new Error('Unsupported chart type');
        }
    }

    // 获取图表类型和转换后的数据
    const chartType = 'line'; // 可以是 'line', 'bar', 'pie', 'scatter', 'map'
    const data = transformDataForChart(chartType, rawData);

    // 指定图表的配置项和数据
    let option;

    switch (chartType) {
        case 'line':
            option = {
                title: {
                    text: '折线图示例',
                    left: 'center'
                },
                tooltip: {
                    trigger: 'axis'
                },
                xAxis: {
                    type: 'category',
                    data: data.map(item => item.name)
                },
                yAxis: {
                    type: 'value'
                },
                series: [{
                    data: data.map(item => item.value),
                    type: 'line'
                }]
            };
            break;
        case 'bar':
            option = {
                title: {
                    text: '柱状图示例',
                    left: 'center'
                },
                tooltip: {
                    trigger: 'axis'
                },
                xAxis: {
                    type: 'category',
                    data: data.map(item => item.name)
                },
                yAxis: {
                    type: 'value'
                },
                series: [{
                    data: data.map(item => item.value),
                    type: 'bar'
                }]
            };
            break;
        case 'pie':
            option = {
                title: {
                    text: '饼图示例',
                    left: 'center'
                },
                tooltip: {
                    trigger: 'item',
                    formatter: '{a} <br/>{b}: {c} ({d}%)'
                },
                legend: {
                    orient: 'vertical',
                    left: 10,
                    data.map(item => item.name)
                },
                series: [
                            {
                            name: '访问来源',
                            type: 'pie',
                            radius: '55%',
                            center: ['50%', '60%'],
                            data: data,
                            emphasis: {
                                itemStyle: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                };
                break;
    case 'scatter':
    option = {
        title: {
            text: '散点图示例',
            left: 'center'
        },
        tooltip: {
            trigger: 'axis'
        },
        xAxis: {},
        yAxis: {},
        series: [{
            symbolSize: 20,
            data,
            type: 'scatter'
        }]
    };
    break;
    case 'map':
    option = {
        title: {
            text: '地图示例',
            left: 'center'
        },
        tooltip: {
            trigger: 'item'
        },
        visualMap: {
            min: 0,
            max: 100,
            left: 'left',
            top: 'bottom',
            text: ['High','Low'],
            calculable : true
        },
        series: [
            {
                name: '中国各城市',
                type: 'map',
                mapType: 'china',
                roam: true,
                label: {
                    show: true
                },
                data
            }
        ]
    };
    break;
    default:
    throw new Error('Unsupported chart type');
    }

    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);
</script>
</body>
</html>


